package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.NLaboratoryAsphaltPenetrometerRes;
import com.ruoyi.zhgd.domain.NLaboratoryAsphaltSofteningPointRes;
import com.ruoyi.zhgd.mapper.NLaboratoryAsphaltPenetrometerResMapper;
import com.ruoyi.zhgd.mapper.NLaboratoryAsphaltSofteningPointResMapper;
import com.ruoyi.zhjd.laboratory.modules.asphaltLowTemperatureElongation.entity.AsphaltLowTemperatureElongationResEntity;
import com.ruoyi.zhjd.laboratory.modules.asphaltLowTemperatureElongation.mapper.AsphaltLowTemperatureElongationResMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryAsphaltSampleBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryAsphaltSampleVo;
import com.ruoyi.zhgd.domain.NLaboratoryAsphaltSample;
import com.ruoyi.zhgd.mapper.NLaboratoryAsphaltSampleMapper;
import com.ruoyi.zhgd.service.INLaboratoryAsphaltSampleService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 试验室沥青样品Service业务层处理
 *
 * @author zyb
 * @date 2024-02-02
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryAsphaltSampleServiceImpl implements INLaboratoryAsphaltSampleService {

    private final NLaboratoryAsphaltSampleMapper baseMapper;

    //针入度
    private final NLaboratoryAsphaltPenetrometerResMapper penetrometerResMapper;
    //软化点
    private final NLaboratoryAsphaltSofteningPointResMapper softeningPointResMapper;
    //延度
    private final AsphaltLowTemperatureElongationResMapper elongationResMapper;


    /**
     * 查询试验室沥青样品
     */
    @Override
    public NLaboratoryAsphaltSampleVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询试验室沥青样品列表
     */
    @Override
    public TableDataInfo<NLaboratoryAsphaltSampleVo> queryPageList(NLaboratoryAsphaltSampleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryAsphaltSample> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryAsphaltSampleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询试验室沥青样品列表
     */
    @Override
    public List<NLaboratoryAsphaltSampleVo> queryList(NLaboratoryAsphaltSampleBo bo) {
        LambdaQueryWrapper<NLaboratoryAsphaltSample> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryAsphaltSample> buildQueryWrapper(NLaboratoryAsphaltSampleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryAsphaltSample> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSampleNo()), NLaboratoryAsphaltSample::getSampleNo, bo.getSampleNo());
        lqw.eq(StringUtils.isNotBlank(bo.getAsphaltType()), NLaboratoryAsphaltSample::getAsphaltType, bo.getAsphaltType());
        lqw.eq(StringUtils.isNotBlank(bo.getAsphaltNo()), NLaboratoryAsphaltSample::getAsphaltNo, bo.getAsphaltNo());
        lqw.eq(bo.getSampleAmount() != null, NLaboratoryAsphaltSample::getSampleAmount, bo.getSampleAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getSampleSite()), NLaboratoryAsphaltSample::getSampleSite, bo.getSampleSite());
        lqw.eq(StringUtils.isNotBlank(bo.getPosition()), NLaboratoryAsphaltSample::getPosition, bo.getPosition());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentNo()), NLaboratoryAsphaltSample::getEquipmentNo, bo.getEquipmentNo());
        lqw.eq(StringUtils.isNotBlank(bo.getQuantity()), NLaboratoryAsphaltSample::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getLotNo()), NLaboratoryAsphaltSample::getLotNo, bo.getLotNo());
        lqw.eq(bo.getProductionDate() != null, NLaboratoryAsphaltSample::getProductionDate, bo.getProductionDate());
        lqw.eq(StringUtils.isNotBlank(bo.getManufacturer()), NLaboratoryAsphaltSample::getManufacturer, bo.getManufacturer());
        lqw.eq(bo.getPenetration() != null, NLaboratoryAsphaltSample::getPenetration, bo.getPenetration());
        lqw.eq(bo.getSofteningPoint() != null, NLaboratoryAsphaltSample::getSofteningPoint, bo.getSofteningPoint());
        lqw.eq(bo.getDuctility() != null, NLaboratoryAsphaltSample::getDuctility, bo.getDuctility());
        lqw.eq(bo.getJudgmentResult() != null, NLaboratoryAsphaltSample::getJudgmentResult, bo.getJudgmentResult());
        return lqw;
    }

    /**
     * 新增试验室沥青样品
     */
    @Override
    @Transactional
    public Boolean insertByBo(NLaboratoryAsphaltSampleBo bo) {
        NLaboratoryAsphaltSample sample = BeanUtil.toBean(bo, NLaboratoryAsphaltSample.class);
        validEntityBeforeSave(sample);
        boolean flag = baseMapper.insert(sample) > 0;
        if (flag) {
            bo.setId(sample.getId());
        }
        String sampleId = sample.getId().toString();
        //绑定针入度数据；
        penetrometerResMapper.updateBatchById(getPenetrometerResList(bo,sampleId));
        //绑定软化度
        NLaboratoryAsphaltSofteningPointRes softeningPointRes = new NLaboratoryAsphaltSofteningPointRes();
        softeningPointRes.setSampleId(sampleId);
        softeningPointRes.setId(bo.getSofteningPointId());
        softeningPointResMapper.updateById(softeningPointRes);
        //绑定延度
        AsphaltLowTemperatureElongationResEntity elongationRes = new AsphaltLowTemperatureElongationResEntity();
        elongationRes.setSampleId(sampleId);
        elongationRes.setId(bo.getDuctilityId());
        elongationResMapper.updateById(elongationRes);
        return flag;
    }


    /**
     * 修改试验室沥青样品
     */
    @Override
    @Transactional
    public Boolean updateByBo(NLaboratoryAsphaltSampleBo bo) {
        NLaboratoryAsphaltSample update = BeanUtil.toBean(bo, NLaboratoryAsphaltSample.class);
        validEntityBeforeSave(update);
        baseMapper.updateById(update);
        //重新绑定针入度、软化度、延度
        String sampleId = bo.getId().toString();
        //绑定针入度数据；
        penetrometerResMapper.updateBatchById(getPenetrometerResList(bo,sampleId));
        //绑定软化度
        NLaboratoryAsphaltSofteningPointRes softeningPointRes = new NLaboratoryAsphaltSofteningPointRes();
        softeningPointRes.setSampleId(sampleId);
        softeningPointRes.setId(bo.getSofteningPointId());
        softeningPointResMapper.updateById(softeningPointRes);
        //绑定延度
        AsphaltLowTemperatureElongationResEntity elongationRes = new AsphaltLowTemperatureElongationResEntity();
        elongationRes.setSampleId(sampleId);
        elongationRes.setId(bo.getDuctilityId());
        elongationResMapper.updateById(elongationRes);
        return true;
    }



    private Collection<NLaboratoryAsphaltPenetrometerRes> getPenetrometerResList(NLaboratoryAsphaltSampleBo bo, String sampleId) {
        List<NLaboratoryAsphaltPenetrometerRes> penetrometerResList = new ArrayList<>();
        NLaboratoryAsphaltPenetrometerRes res1 = new NLaboratoryAsphaltPenetrometerRes();
        res1.setSampleId(sampleId);
        res1.setId(bo.getPenetrationId1());
        penetrometerResList.add(res1);
        NLaboratoryAsphaltPenetrometerRes res2 = new NLaboratoryAsphaltPenetrometerRes();
        res2.setSampleId(sampleId);
        res2.setId(bo.getPenetrationId2());
        penetrometerResList.add(res2);
        NLaboratoryAsphaltPenetrometerRes res3 = new NLaboratoryAsphaltPenetrometerRes();
        res3.setSampleId(sampleId);
        res3.setId(bo.getPenetrationId3());
        return penetrometerResList;
    }
    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryAsphaltSample entity){
        //TODO 做一些数据校验,如唯一约束

    }

    /**
     * 批量删除试验室沥青样品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
